//
//  XQTabItem.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/3.
//

import UIKit

class XQTabItem: UIView {
    
    private let titleLabel = UILabel()
    private let imageView = UIImageView()
    
    private var normalImage : UIImage?
    private var selectImage : UIImage?
    
    var isSelected: Bool = false {
        didSet {
            titleLabel.textColor = isSelected ? .mainTextColor : .tabNormalColor
            titleLabel.font = isSelected ? UIFont.systemFont(ofSize: 10, weight: .medium) : UIFont.systemFont(ofSize: 10)
            imageView.image = isSelected ? selectImage : normalImage
        }
    }

    init(title: String, image: UIImage?, selectedImage: UIImage?) {
        super.init(frame: .zero)
        normalImage = image
        selectImage = selectedImage
        setupViews(title: title, image: image)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupViews(title: String, image: UIImage?) {
        titleLabel.text = title
        titleLabel.font = UIFont.systemFont(ofSize: 10)
        titleLabel.textAlignment = .center
        
        imageView.image = image
        imageView.contentMode = .scaleAspectFit
        
        addSubview(imageView)
        addSubview(titleLabel)
       
        imageView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(4.5)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(24)
        }

        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(imageView.snp.bottom).offset(2)
            make.centerX.equalToSuperview()
        }
    }
}
